﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.Admin
{
    public partial class Bulletin : System.Web.UI.Page
    {
        BulletinBl bulletinBl = new BulletinBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            gvList.DataSource = bulletinBl.GetAll();
            gvList.DataBind();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        { //点击编辑
            if (e.CommandName == "EditUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("BulletinEdit.aspx?id=" + id);

            }
            //点击删除
            else if (e.CommandName == "DeleteUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                try
                {
                    bulletinBl.Delete(id);
                    LoadData();
                    MessageHelper.RegShowJSAjax(gvList, "删除公告成功", this.Page);
                }
                catch (ReferDataNotExistException)
                {
                    AlertHelper.ShowError("发生错误，请联系系统管理员！", this.Page);
                }
            }
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewHelper.AddRowNumber(e);
        }

        protected void pager_PageChanged(object sender, EventArgs e)
        {
            gvList.PageIndex = pager.CurrentPageIndex - 1;
            LoadData();
        }
    }
}